import { _decorator, Component, Vec3 } from "cc";
import { FSMManager } from "../manager/FSMManager";
import {
  IShootStrategy,
  SingleShot,
  SpreadShot,
  LaserShot,
} from "../strategy/ShootStrategy";
import { ShootState } from "../enums/GameEnums";
const { ccclass, property } = _decorator;

@ccclass("Player")
export class Player extends Component {
  private _stateMachine = null;
  private _currentStrategy: IShootStrategy = null;

  onLoad() {
    // 创建状态机
    const fsmManager = new FSMManager();
    this._stateMachine = fsmManager.createStateMachine("PlayerShootState");

    // 注册射击状态
    this._stateMachine.registerState(ShootState.SINGLE, {
      onEnter: () => {
        this._currentStrategy = new SingleShot();
        console.log("Switched to single shot mode");
      },
    });

    this._stateMachine.registerState(ShootState.SPREAD, {
      onEnter: () => {
        this._currentStrategy = new SpreadShot();
        console.log("Switched to spread shot mode");
      },
    });

    this._stateMachine.registerState(ShootState.LASER, {
      onEnter: () => {
        this._currentStrategy = new LaserShot();
        console.log("Switched to laser mode");
      },
    });

    // 设置初始状态
    this._stateMachine.changeState(ShootState.SINGLE);
  }

  // 射击方法
  shoot(position: Vec3, direction: Vec3) {
    if (this._currentStrategy) {
      this._currentStrategy.shoot(position, direction);
    }
  }

  // 切换射击模式
  changeShootMode(mode: ShootState) {
    this._stateMachine.changeState(mode);
  }
}
